package com.neuedu.dao;

import com.neuedu.common.DBUtils;
import com.neuedu.entity.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @标 题： 龙猫电商管理平台
 * @项目名称： YD-WebShop
 * @描 述： 管理员数据访问类
 * @作 者： 侯建军
 * @日 期： 2019/6/18  16:25
 */
public class AdminDao {
    public boolean insert(Admin admin) {
        String sql = "INSERT INTO admin (adminName,password,createtime) VALUES(?,?,?)";
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, admin.getAdminName());
            ps.setString(2, admin.getPassword());
            //数据类型转换，将java.util.Date 转为java.sql.Date
            ps.setDate(3, new java.sql.Date(admin.getCreatetime().getTime()));
            if (ps.executeUpdate() > 0) {
                DBUtils.close(connection, ps, null);
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public List<Admin> findByAll() {
        String sql = "SELECT * FROM admin";
        List<Admin> list = new ArrayList<>();
        Connection connection = null;
        try {
            connection = DBUtils.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql);
            //查询数据库得到结果集
            ResultSet rs = ps.executeQuery();
            //数据库表到对象转换
            while (rs.next()) {
//               System.out.print(rs.getInt(1));
//               System.out.println(rs.getString(2));
                Admin admin = new Admin();
                admin.setId(rs.getInt(1));
                admin.setAdminName(rs.getString(2));
                admin.setPassword(rs.getString(3));
                admin.setCreatetime(rs.getTimestamp(4));
                list.add(admin);
            }
            DBUtils.close(connection, ps, rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 修改，先查后改
     *
     * @return
     */
    public boolean update(Admin admin) {
        Admin admin1 = this.getById(admin.getId());
        if (null != admin1) {
            try {
                String sql = " UPDATE admin set adminName=?,password=?," +
                        "updatetime=?,lastlogintime=? WHERE id=?";
                Connection connection = DBUtils.getConnection();
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, admin.getAdminName());
                ps.setString(2, admin.getPassword());
                ps.setDate(3, new java.sql.Date(admin.getCreatetime().getTime()));
                ps.setDate(4, new java.sql.Date(admin.getLastlogintime().getTime()));
                ps.setInt(5, admin.getId());
                if (ps.executeUpdate() > 0) {
                    DBUtils.close(connection, ps, null);
                    return true;
                }
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
        return false;
    }

    /**
     * 根据ID获取一个对象
     *
     * @return
     */
    public Admin getById(Integer id) {
        String sql = "SELECT id,adminName,password,createtime,updatetime,lastlogintime " +
                " FROM  admin WHERE id=?";
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql);
            //传一个ID参数
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            Admin admin = new Admin();
            if (rs.next()) {
                admin.setId(rs.getInt(1));
                admin.setAdminName(rs.getString(2));
                admin.setPassword(rs.getString(3));
                admin.setCreatetime(rs.getTimestamp(4));
                admin.setUpdatetime(rs.getTimestamp(5));
                admin.setLastlogintime(rs.getTimestamp(6));
                DBUtils.close(connection, ps, rs);
                return admin;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.getStackTrace();
            return null;
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    public boolean delete(Integer[] ids) {
        String sql = "DELETE FROM admin WHERE id IN (";
        try {
            for (Integer id : ids) {
                sql += "?,";
            }
            sql = sql.substring(0, sql.length() - 1) + ")";
            System.out.print(sql);
            Connection connection = DBUtils.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql);
            int count = ids.length;
            for (int i = 0; i < count; i++) {
                ps.setInt(i + 1, ids[i]);
            }
            if (ps.executeUpdate() > 0) {
                DBUtils.close(connection, ps, null);
                return true;
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        return false;
    }

    /**
     * 条件查询
     * @param where
     * @return
     */
    public List<Admin> findByWhere(String where) {
        String sql = "SELECT id,adminName,password,createtime,updatetime,lastlogintime\n" +
                " FROM  admin WHERE 1=1 " + where;
        List<Admin> list = new ArrayList<>();
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Admin admin = new Admin();
                admin.setId(rs.getInt(1));
                admin.setAdminName(rs.getString(2));
                admin.setPassword(rs.getString(3));
                admin.setCreatetime(rs.getTimestamp(4));
                admin.setUpdatetime(rs.getTimestamp(5));
                admin.setLastlogintime(rs.getTimestamp(6));
                list.add(admin);
            }
            DBUtils.close(connection, ps, rs);
        } catch (Exception e) {
            e.getStackTrace();
        }
        return list;
    }

    public static void main(String[] args) {
        AdminDao adminDao = new AdminDao();
//        Admin admin=new Admin();
//        admin.setAdminName("zhangsan");
//        admin.setPassword("000000");
//        admin.setCreatetime(new Date());
//        adminDao.insert(admin);
//        List<Admin> admins = adminDao.findByAll();
//        for (Admin admin : admins) {
//            System.out.println(admin.getId() + "->" + admin.getAdminName());
//        }
//        Admin admin=adminDao.getById(2);
//        if(null!=admin){
//            System.out.print(admin.getAdminName());
//        }
//        Admin admin=new Admin();
//        admin.setId(2);
//        admin.setAdminName("zhangsan");
//        admin.setPassword("000000");
//        admin.setCreatetime(new Date());
//        admin.setLastlogintime(new Date());
//        adminDao.update(admin);
//        Integer[] ids = {7, 8};
//        adminDao.delete(ids);
        Admin admin=new Admin();
        admin.setAdminName("zhangsan");
        admin.setPassword("000");
//        List<Admin> list=adminDao.findByWhere(" AND adminName='"+ admin.getAdminName()+"'");
        String where=" AND adminName='"+ admin.getAdminName()+"' AND password='"+admin.getPassword()+"'";
        List<Admin> list=adminDao.findByWhere(where);
        if(list.size()>0){
//            System.out.print("该用户已存在");
            System.out.print("登录成功");
        }
    }
}
